package net.tycmc.cty.ctyms.timejob.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;

public class ListUtil {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public ListUtil(){
		
	}
	public static String[] get(List<Map<String,Object>> list,String parm){
		String[] parmDef = new String[2];
		for (Map<String,Object> curr : list) {
			if(StringUtil.isNullOrEmpty(MapGetter.getString(curr, parm))){
				parmDef[0] = MapGetter.getString(curr, parm);
				break;
			}
		}
		for(int i=list.size()-1;i>=0;i--){
			if(StringUtil.isNullOrEmpty(MapGetter.getString(list.get(i), parm))){
				parmDef[1] = MapGetter.getString(list.get(i), parm);
				break;
			}
		}
		return parmDef;
	}
	/**
	 * 按照指定的map.key 将List组装为Map<key,List<Map<String,Object>>>
	 * @author haoyang
	 * 
	 **/
	public Map<String,List<Map<String,Object>>> groupBy(String key1,List<Map<String,Object>> srcList){
		Map<String,List<Map<String,Object>>> retMap = new HashMap<String, List<Map<String,Object>>>();
		String mapKey = "";
		List<Map<String,Object>> mapList = null;
		if(null!=srcList&&srcList.size()>0){
			for (Map<String, Object> map : srcList) {
				mapKey = MapGetter.getString(map, key1);
				mapList = retMap.get(mapKey);
				if(mapList==null){
					mapList = new ArrayList<Map<String,Object>>();
					mapList.add(map);
					retMap.put(mapKey.toString(), mapList);
				}else{
					mapList.add(map);
				}
			}
		}
		return retMap;
	}
	/**
	 * 按照指定的map.key1_map.key2  将List组装为Map<map.key1_map.key2,List<Map<String,Object>>>
	 * @author jiyongtian
	 * 
	 **/
	public Map<String, List<Map<String,Object>>> groupByKeys(String key1,
			String key2, List<Map<String, Object>> srcList) {
		Map<String,List<Map<String,Object>>> retMap = new HashMap<String, List<Map<String,Object>>>();
		List<Map<String,Object>> mapList = null;
		if(null!=srcList&&srcList.size()>0){
			for (Map<String, Object> map : srcList) {
				StringBuffer mapKey = new StringBuffer();
				mapKey.append(MapGetter.getString(map, key1)+"_"+ MapGetter.getString(map, key2));
				mapList = retMap.get(mapKey.toString());
				if(null==mapList||mapList.size()<=0){
					mapList = new ArrayList<Map<String,Object>>();
					mapList.add(map);
					retMap.put(mapKey.toString(), mapList);
				}else{
					mapList.add(map);
				}
			}
		}
		return retMap;
	}
	/**
	 *如果arg0中不存在arg1的key，则新增，并赋值为空串“”
	 * @param Map<String, Object> arg0
	 * @param Map<String, Object> arg1
	 * 
	 **/
	public void makeParam(Map<String, Object> arg0,Map<String, Object> arg1) {
		Iterator<Map.Entry<String, Object>> it = arg1.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> entry = it.next();
			if(arg0.containsKey(entry.getKey())){
				
			}else{
				arg0.put(entry.getKey(), "");
			}
		}
	}
	/**
	 *获取List<<Map<String,Object>>中指定key对应的value的最大值  
	 *@author jiyongtian
	 **/
	public int getDataMaxVal(List<Map<String,Object>> srcList,String key) {
		List<Integer> tempInt = new ArrayList<Integer>();
		if(null!=srcList){
			Iterator<Map<String,Object>> it = srcList.iterator();
			while(it.hasNext()){
				Map<String,Object> next = it.next();
				tempInt.add(Integer.parseInt(next.get(key).toString()));
			}	
		}
		if(!tempInt.isEmpty()&&tempInt.size()>0){
			return Collections.max(tempInt);
		}else{
			return 0;
		}
	}
	/**
	 * List<Map<String,Object>>排序 仅支持Map.value 类型为int\date
	 * @param String keyField 排序时使用的Map.key
	 * @param boolean isTime 该key对应的value是否是“时间” 
	 * @param boolean desc 是否是倒叙
	 * @author jiyongtian 2014-03-17
	 **/
	public void sortList(List<Map<String,Object>> srcList, final String keyField, final boolean isTime,final boolean desc){
		Collections.sort(srcList, new Comparator<Map<String,Object>>() {
			@Override
			public int compare(Map<String, Object> arg0,Map<String, Object> arg1) {
				int i = 0;
				if (isTime)
					try
					{
						long l = 0;
						if(desc){
							l = ListUtil.sdf.parse(arg1.get(keyField).toString()).getTime() - ListUtil.sdf.parse(arg0.get(keyField).toString()).getTime();
						}else{
							l= ListUtil.sdf.parse(arg0.get(keyField).toString()).getTime() - ListUtil.sdf.parse(arg1.get(keyField).toString()).getTime();
						}
						if (l > 0L){
							i = 1;
						}else if (l == 0L){
							i = 0;
						}else{
							i = -1;
						}
					}catch (ParseException parseexception) { 
						
					}
				else{
					try
					{
						int turn0 = Integer.parseInt(arg0.get(keyField).toString());
						int turn1 = Integer.parseInt(arg1.get(keyField).toString());
						if(desc){
							i = turn1 - turn0;
						}else{
							i = turn0 - turn1;
						}
					}catch (NumberFormatException numberformatexception) { 
						
					}catch (NullPointerException nullpointerexception) { 
						
					}
				}
				return i;
			}
		});
	}
}
